/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.Actions;

import controller.Forms.UpDateAccountantForm;
import dao.AccountantDAO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Accountant;
import model.Erro;
import model.User;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Adriano
 */
public class UpDateAccountantAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {

            User user = (User) request.getSession().getAttribute("User");

            if (user == null) {
                request.getSession().setAttribute("ErroMSG", Erro.erroMsg[Erro.NAO_AUTENTICADO]);
                return mapping.findForward("erroAlias");
            } else {

                Accountant a = new Accountant();

                UpDateAccountantForm aForm = (UpDateAccountantForm) form;
                a.setTxtNome(aForm.getTxtNome());
                a.setTxtCpf(aForm.getTxtCpf());
                a.setTxtCrc(aForm.getTxtCrc());
                a.setLstAssinatura(aForm.getLstAssinatura());
                a.setLstUF(aForm.getLstUF());
                a.setTxtId(aForm.getTxtId());

                AccountantDAO userDAO = new AccountantDAO();
                userDAO.update(a);
                userDAO.closeDAO();

                return mapping.findForward("homeAlias");
            }

        } catch (Exception e) {

            System.out.println("UPDATEACCOUNTANTCATION - " + Erro.erroMsg[Erro.DADOS_INVÁLIDOS] + e.getMessage());
            return mapping.findForward("erroAlias");
        }

    }

}
